package com.ajr.core.visma;

import java.util.Enumeration;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.ajr.core.Settings;
import com.jnr.servlets.SubmitOrder;

import se.codevision.jinterlink.JInterLinkException;
import se.codevision.jinterlink.registers.CodeOfLanguage;
import se.codevision.jinterlink.registers.CodeOfTermsOfPayment;
import se.codevision.jinterlink.registers.JInterLinkRegisterFactory;

public class VismaEnvironment {
	Logger logger = Logger.getLogger(VismaEnvironment.class);

	
	HashMap languageCodes = new HashMap();
	
//	double vismaVat = 25;
	
	
	public HashMap getLanguageCodes() {
		return languageCodes;
	}


	public static VismaEnvironment instance = new VismaEnvironment();
	
	
	private VismaEnvironment() {
		
		
		
	}
	
	private void readLanguageCodes() {
		CodeOfLanguage codeOfWayOfPayment = null;
		try {
			codeOfWayOfPayment = JInterLinkRegisterFactory.createCodeOfLanguage();
//			codeOfWayOfPayment.firstRecord();
//			System.out.println(codeOfWayOfPayment.getPaymentCode() + ":" + codeOfWayOfPayment.getCode());
			Enumeration e = codeOfWayOfPayment.elements();
			System.out.println("elements "+e.hasMoreElements());
			
			while (e.hasMoreElements()) {
				CodeOfTermsOfPayment object = (CodeOfTermsOfPayment) e.nextElement();
				
				languageCodes.put(object.getText(), object.getCode());
				
//				System.out.println(object.getText() + ":" + object.getCode());
				
			}
			
			codeOfWayOfPayment.release();
			
		} catch (JInterLinkException e) {
			logger.error("Error while reading language codes",e);
		}
		
	}

	
	
	public String getPaymentCodes(String paymentMethod) {
		String paymentcode = Settings.getInstance().getDefaultPaymentMethod();
		
		boolean found = false;
		
		CodeOfTermsOfPayment codeOfWayOfPayment = null;
		try {
			codeOfWayOfPayment = JInterLinkRegisterFactory.createCodeOfTermsOfPayment();
			
//			codeOfWayOfPayment.firstRecord();
//			System.out.println(codeOfWayOfPayment.getPaymentCode() + ":" + codeOfWayOfPayment.getCode());
			Enumeration e = codeOfWayOfPayment.elements();
			logger.info("elements "+e.hasMoreElements());
			
			while (e.hasMoreElements()) {
				CodeOfTermsOfPayment object = (CodeOfTermsOfPayment) e.nextElement();
				
				
				if(paymentMethod.equalsIgnoreCase(object.getText())) {
					paymentcode =  object.getCode();
					found = true;
					break;
				}
				
//				System.out.println("elements *** ");
				
//				System.out.println(object.getText() + ":" + object.getCode());
				
			}
			
			
		} catch (JInterLinkException e) {
			// TODO Auto-generated catch block
			logger.error("Error while reading code of payments", e);
		}finally {
			codeOfWayOfPayment.release();
		}
		
		if(!found) {
			logger.error("payment method not availabe "+paymentMethod);
		}

		return paymentcode;
	}



	public static VismaEnvironment getInstance() {
		return instance;
	}

	
	public static void main(String[] args) {
		
	}
	
}
